#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

typedef struct LNode *PtrToNode;
struct LNode {
    int data;
    PtrToNode next;
};
typedef PtrToNode List;
typedef PtrToNode Position;

/*尾插法———利用尾指针插入新节点*/
void createlistR(List &C, int a[], int n)
{
    Position s, r;

    C = (List)malloc(sizeof(struct LNode));  //分配头结点
    C->next = NULL;                          //头结点初始状态指向NULL
    r = C;                                   //尾指针初始状态指向头结点
    for (int i = 0; i < n; i++)
    {
        s = (Position)malloc(sizeof(struct LNode));     //分配新节点
        s->data = a[i];                                 //新节点赋值
        r->next = s;                                    //最后一个结点指向新节点
        r = s;                                          //尾指针指向新节点
    }                                                   
    r->next = NULL;                                     //r就是尾指针
}

/*头插法*/
void createlistF(List &C, int a[], int n)
{
    Position s;

    C = (List)malloc(sizeof(struct LNode));             //分配头节点
    C->next = NULL;                                     //头结点初始状态指向NULL
    for (int i = 0; i < n; i++)
    {
        s = (Position)malloc(sizeof(struct LNode));     //分配新节点
        s->data = a[i];                                 //新节点赋值
        s->next = C->next;                              //新节点指向头结点指向的结点
        C->next = s;                                    //头结点指向新节点
    }
}

void Display(List L)
{
    for (Position ptr = L->next; ptr != NULL; ptr = ptr->next)
        printf("%d  ", ptr->data);
    printf("\n");
}

int main()
{
    List list_R, list_F;
    int size = 5, a[size] = {1, 3, 5, 7, 9};

    createlistR(list_R, a, size);
    createlistF(list_F, a, size);

    Display(list_R);
    Display(list_F);

    return 0;
}